package com.spring.daily.web.controller;

import javax.annotation.Resource;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.spring.daily.web.dto.DailyParamter;
import com.spring.daily.web.model.CourseClassBaby;
import com.spring.daily.web.model.DailyRecordBase;
import com.spring.daily.web.model.WeeklyRecordBase;
import com.spring.daily.web.model.WeeklyRecordWithBLOBs;
import com.spring.daily.web.service.DailyService;
import com.spring.security.core.annotation.CheckParamterValidity;
import com.spring.security.core.annotation.CheckToken;
import com.spring.security.core.validators.Update;
import com.spring.security.web.controller.BaseController;
import com.spring.security.web.dao.JedisCache;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value="/daily")
public class DailyController extends BaseController{
	
	@Resource private DailyService dailyService;
	
	@Resource private JedisCache jedisCache;
	
    @RequestMapping(value="/daily", method = RequestMethod.GET)
    @ApiOperation(value = "获取观察日记记录", httpMethod = "GET", response = Object.class, notes = "获取观察日记记录",produces = "application/json; charset=utf-8")
    @CheckToken
    public Object daily(
    		@ApiParam(required = true, name = "token", value = "用户session") @RequestParam String token,
    		DailyParamter dailyParamter){
		try {
			return dailyService.daily(dailyParamter);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		} 
    }
    
    @RequestMapping(value="/dailyDetail", method = RequestMethod.GET)
    @ApiOperation(value = "获取观察日记记录", httpMethod = "GET", response = Object.class, notes = "获取观察日记记录",produces = "application/json; charset=utf-8")
    public Object dailyDetail(
    		@ApiParam(required = true, name = "dailyRecordID", value = "dailyRecordID") @RequestParam String dailyRecordID,
    		@ApiParam(required = false, name = "token", value = "token") @RequestParam(required=false) String token
    		){
		try {
			return dailyService.dailyDetail(dailyRecordID,token);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		} 
    }
    
    @RequestMapping(value="/delDaily", method = RequestMethod.POST)
    @ApiOperation(value = "删除观察日记记录", httpMethod = "POST", response = Object.class, notes = "删除观察日记记录",produces = "application/json; charset=utf-8")
    public Object delDaily(
    		@ApiParam(required = true, name = "dailyRecordID", value = "dailyRecordID") @RequestParam String dailyRecordID
    		){
		try {
			return dailyService.delDaily(dailyRecordID);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		} 
    }
    
    @RequestMapping(value="/weeklyDailyDetail", method = RequestMethod.GET)
    @ApiOperation(value = "获取观察周记记录", httpMethod = "GET", response = Object.class, notes = "获取观察周记记录",produces = "application/json; charset=utf-8")
    public Object weeklyDailyDetail(
    		@ApiParam(required = true, name = "weeklyRecordID", value = "weeklyRecordID") @RequestParam String weeklyRecordID,
    		@ApiParam(required = false, name = "token", value = "token") @RequestParam(required=false) String token    		
    		){
		try {
			BaseResult object=dailyService.weeklyDetail(weeklyRecordID,token);
			return object;
		} catch (Exception e) {	
			e.printStackTrace();
			return MyTools.analyseError(e);
		} 
    }   
    
    @RequestMapping(value="/delWeeklyRecord", method = RequestMethod.POST)
    @ApiOperation(value = "删除观察周记记录", httpMethod = "POST", response = Object.class, notes = "删除观察周记记录",produces = "application/json; charset=utf-8")
    public Object delWeeklyRecord(
    		@ApiParam(required = true, name = "weeklyRecordID", value = "weeklyRecordID") @RequestParam String weeklyRecordID
    		){
		try {
			return dailyService.delWeeklyRecord(weeklyRecordID);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		} 
    }
    
    @RequestMapping(value="/weeklyDaily", method = RequestMethod.POST)
    @ApiOperation(value = "修改观察记录", httpMethod = "POST", response = Object.class, notes = "修改观察记录",produces = "application/json; charset=utf-8")
    @CheckParamterValidity
    public Object weeklyDaily(
    		@RequestBody @Validated({Update.class}) WeeklyRecordWithBLOBs weeklyRecord,BindingResult result
    		){
		try {
			return dailyService.weeklyDaily(weeklyRecord);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		}
    }   

    @RequestMapping(value="/class", method = RequestMethod.GET)
    @ApiOperation(value = "查询班级", httpMethod = "GET", response = Object.class, notes = "查询班级",produces = "application/json; charset=utf-8")
    public Object courseClass(){
		try {
			return dailyService.courseClass();
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		}
    }
    
    @RequestMapping(value="/baby", method = RequestMethod.GET)
    @ApiOperation(value = "baby列表", httpMethod = "GET", response = Object.class, notes = "baby列表",produces = "application/json; charset=utf-8")
    public Object baby(CourseClassBaby  courseClassBaby){
		try {
			return dailyService.baby(courseClassBaby);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		}
    }  

    @RequestMapping(value="/dailyType", method = RequestMethod.GET)
    @ApiOperation(value = "活动项目列表", httpMethod = "GET", response = Object.class, notes = "活动项目列表",produces = "application/json; charset=utf-8")
    @CheckToken
    public Object dailyType(
    		@ApiParam(required = true, name = "token", value = "用户session") @RequestParam String token
    		){
		try {
			return dailyService.dailyType(token);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		}
    }  
    
    @RequestMapping(value="/daily", method = RequestMethod.POST)
    @ApiOperation(value = "发布日志", httpMethod = "POST", response = Object.class, notes = "发布日志",produces = "application/json; charset=utf-8")
    public Object daily(@RequestBody DailyRecordBase daily){
		try {
			return dailyService.daily(daily);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		}
    } 
    
    @RequestMapping(value="/weeklyQuestion", method = RequestMethod.GET)
    @ApiOperation(value = "获取周记问题", httpMethod = "GET", response = Object.class, notes = "获取周记问题",produces = "application/json; charset=utf-8")
    public Object weeklyQuestion(
    		@ApiParam(required = true, name = "parentWeeklyRecordTypeID", value = "父周记问题类型id") @RequestParam(required=false) String parentWeeklyRecordTypeID
    		){
		try {
			return dailyService.weeklyQuestion(parentWeeklyRecordTypeID);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		}
    }
    
    @RequestMapping(value="/addWeeklyDaily", method = RequestMethod.POST)
    @ApiOperation(value = "发布周记", httpMethod = "POST", response = Object.class, notes = "发布周记",produces = "application/json; charset=utf-8")
    public Object weeklyDaily(@RequestBody WeeklyRecordBase weeklyRecordBase){
		try {
			return dailyService.weeklyDaily(weeklyRecordBase);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		}
    } 
    
    @RequestMapping(value="/teacher", method = RequestMethod.GET)
    @ApiOperation(value = "获取教师列表", httpMethod = "GET", response = Object.class, notes = "获取教师列表",produces = "application/json; charset=utf-8")
    public Object teacher(
    		@ApiParam(required = true, name = "courseClassid", value = "班级id") @RequestParam(required=false) String courseClassid
    		){
		try {
			return dailyService.teacher(courseClassid);
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		}
    } 
    
    @RequestMapping(value="/parentWeeklyRecordType", method = RequestMethod.GET)
    @ApiOperation(value = "获取日志父类别", httpMethod = "GET", response = Object.class, notes = "获取教师列表",produces = "application/json; charset=utf-8")
    public Object parentWeeklyRecordType(){
		try {
			return dailyService.selectParent();
		} catch (Exception e) {			
			return MyTools.analyseError(e);
		}
    } 
}
